<template>
    <div class="honorrollcontainer">
        <!-- 荣誉榜管理 -->
        <div class="honorrollMentMain">
            <el-row>
                <el-col :span="24">
                    <el-form
                        :inline="true"
                        :model="formData"
                        class="demo-form-inline formData_form_inline"
                        size="small"
                    >
                        <el-form-item label="荣誉榜顺序">
                            <el-input v-model="formData.goldOrder" placeholder="请输入荣誉榜顺序"></el-input>
                        </el-form-item>
                        <el-form-item label="荣誉榜内容">
                            <el-input v-model="formData.goldList" placeholder="请输入荣誉榜内容"></el-input>
                        </el-form-item>
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-search"
                            @click="hanlesearch"
                            :loading="searchLoading"
                            class="searchBtn"
                        >查询</el-button>
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-refresh-right"
                            @click="handleReset"
                            :loading="resetLoading"
                            class="resetBtn"
                        >重置</el-button>
                    </el-form>
                    <div style="margin-bottom: 16px;">
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-circle-plus-outline"
                            @click="hanlePush"
                        >新增</el-button>
                    </div>
                    <el-table
                        :data="tableData"
                        width="100%"
                        border
                        highlight-current-row
                        stripe
                        class="heard-bg"
                    >
                        <el-table-column label="荣誉榜顺序" prop="goldOrder" width="160px"></el-table-column>
                        <el-table-column label="荣誉榜内容" prop="goldList"></el-table-column>
                        <el-table-column label="操作" width="250">
                            <template slot-scope="scope">
                                <el-button
                                    type="primary"
                                    icon="el-icon-edit"
                                    title="编辑"
                                    circle
                                    @click="handleEdit(scope.row)"
                                />
                                <el-button
                                    type="danger"
                                    icon="el-icon-delete"
                                    title="删除"
                                    circle
                                    @click="handleDelete(scope.row)"
                                />
                            </template>
                        </el-table-column>
                    </el-table>
                    <!-- 分页 -->
                    <div class="pagination">
                        <Pagination
                            :hanleChangeCurrentPage="hanleChangeCurrentPage"
                            :handleChangeSize="handleChangeSize"
                            :currentPage="currentPage"
                            :pageSize="pageSize"
                            :total="total"
                        />
                    </div>
                </el-col>
            </el-row>
        </div>
        <!--添加/编辑-->
        <el-dialog
            v-drag
            :visible.sync="Show"
            :close-on-click-modal="false"
            append-to-body
            center
            width="800px"
        >
            <div slot="title">{{EditTitle}}</div>
            <el-form
                :model="Edit"
                label-position="right"
                ref="Edit"
                label-width="110px"
                :rules="formRules"
                size="small"
            >
                <el-form-item label="荣誉榜顺序" prop="goldOrder">
                    <el-input v-model="Edit.goldOrder" placeholder="请输入" style="width: 90%;" />
                </el-form-item>
                <el-form-item label="荣誉榜内容" prop="goldList" style="margin-bottom:10px;">
                    <el-input
                        type="textarea"
                        :autosize="{minRows:4,maxRows:4}"
                        v-model="Edit.goldList"
                        placeholder="请输入"
                        style="width: 80%; height: 100px"
                    />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button
                    size="small"
                    icon="el-icon-circle-close"
                    @click="Show = false"
                    class="cancelBtn"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="submitEdit"
                    class="submitBtn"
                    icon="el-icon-circle-check"
                >确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import Pagination from '@/components/Pagination/Pagination.vue';

export default {
    name: 'honorroll',
    components: {
        Pagination,
    },
    data() {
        return {
            defaultProps: {
                parent: 'pid',
                value: 'id',
                label: 'deptName',
                children: 'child',
            },
            formData: {
                goldOrder: '',
                goldList: '',
            },
            pageLength: 0,
            // 新增字段
            Edit: {
                goldOrder: null,
                goldList: null,
            },
            EditTitle: '',
            sendTitle: '',
            Show: false,
            sendShow: false,
            // 搜索loading
            searchLoading: false,
            // 重置loading
            resetLoading: false,
            formRules: {
                goldOrder: [
                    { required: true, message: '请输入荣誉榜顺序', trigger: 'blur' },
                ],
                goldList: [
                    { required: true, message: '请输入荣誉榜内容', trigger: 'blur' },
                ],
            },
            // 当前页码
            currentPage: 1,
            // 页码大小
            pageSize: 10,
            // 总数
            total: 0,
            // 当前页码
            currentPageDetail: 1,
            // 页码大小
            pageSizeDetail: 10,
            // 总数
            totalDetail: 0,
            // 表格内容
            tableData: [],
        };
    },
    mounted() {
        this.getInitData(this.formData);
    },
    filters: {
        filterValue(val) {
            return val.replace(/<[^>]+>/g, '');
        },
    },
    methods: {
        // 获取荣誉榜数据
        getInitData(form) {
            let params = {
                pageNum: this.currentPage,
                pageSize: this.pageSize,
            };
            params = Object.assign({}, params, form);
            this.$post.honor.pageHonor(params)
                .then((res) => {
                    if (res.data.success) {
                        this.tableData = res.data.content.records;
                        this.total = res.data.content.total;
                    }
                });
        },
        // 点击搜索
        hanlesearch() {
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击重置
        handleReset() {
            this.formData = {
                goldOrder: '',
                goldList: '',
            };
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击新增
        hanlePush() {
            // 解决一进来就进行表单验证问题
            this.EditTitle = '添加荣誉信息';
            this.Edit = {
                goldOrder: null,
                goldList: null,
            };
            this.pageLength = 0;
            this.Show = true;
            // 解决一进来就进行表单验证问题
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 点击编辑
        handleEdit(row) {
            this.EditTitle = '编辑荣誉榜';
            this.Edit = JSON.parse(JSON.stringify(row));
            this.Show = true;
            // 解决一进来就进行表单验证问题
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 单个删除
        handleDelete(row) {
            this.$confirm('确认删除该荣誉信息？', '提示', { customClass: 'warning', type: 'warning' })
                .then(() => {
                    this.$post.honor.deleteHonor({ id: row.id })
                        .then((res) => {
                            if (res.data.success) {
                                this.$message.success('删除成功！');
                                if (this.tableData.length === 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getInitData(this.formData);
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        });
                })
                .catch(() => {
                });
        },
        // 新增点击确定按钮
        submitEdit() {
            console.log(this.Edit);
            if (this.Edit.id) {
                // 编辑
                this.$refs.Edit.validate((valid) => {
                    console.log(valid);
                    if (valid) {
                        this.$post.honor.updateHonor(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('修改成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            } else {
                // 添加
                this.$refs.Edit.validate((valid) => {
                    if (valid) {
                        this.$post.honor.addHonor(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('添加成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            }
        },
        // 页码切换方法
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getInitData(this.formData);
        },
        // 切换分页大小方法
        handleChangeSize(val) {
            this.pageSize = val;
            this.getInitData(this.formData);
        },
    },
};
</script>

<style  lang="scss">
.editor .ql-container {
    height: 280px;
}
.honorrollcontainer {
    width: 100%;
    height: 100%;
    #grid-content /deep/ .side-bar-scrollbar-wrap {
        max-height: 500px;
        min-height: 200px;
    }
    .rolemanageMentHeader {
        height: 68px;
        background: #fff;
        border-radius: 5px;
        text-align: left;
        font-size: 14px;
        line-height: 68px;
        padding: 0 12px;
        .el-form--inline .el-form-item__content {
            vertical-align: middle;
        }
        .el-button {
            i {
                font-weight: bolder;
                text-align: left;
                font-size: 16px;
                margin-left: -6px;
            }
        }
    }

    .honorrollMentMain {
        width: 100%;
        background: #fff;
        margin: 0 0 15px;
        // border-radius:5px;
        padding: 20px;
        box-sizing: border-box;
        text-align: left;

        .tableTitle {
            width: 100%;
            height: 48px;
            border: 1px solid #eee;
            border-bottom: none;
            line-height: 48px;
            margin-top: 20px;
            color: #666;
        }
        .pagination {
            margin-top: 20px;
            text-align: right;
        }
    }
}
</style>
